* Table 2 
* effects on PV earnings when shocks at 30, 40, 50, 60

qui{
* age 30
qui{
clear 
use "Experiments Stata Files/Experiment09.dta"
append using "Experiments Stata Files/Experiment013.dta"
append using "Experiments Stata Files/Experiment029.dta"

keep if age<=64
drop if age<30


bysort experiment ID: gen insample=1 if (H==2 | H==3) & age==30
replace insample=0 if insample==.
bysort experiment ID: egen insample_new= max(insample)
keep if insample_new==1

gen Experience=hours/40
sort experiment ID age
by experiment ID: egen  Experience_total = total(Experience)
drop Experience
rename Experience_total Experience
replace Experience=. if age>30

sort experiment ID age
gen time_period = age-30
generate discounting_factor=0.956937799^(time_period)  
gen discounted_earnings=earnings*discounting_factor
bysort experiment ID: egen  PV_earnings = total(discounted_earnings)
replace  PV_earnings=. if age>30


preserve
* all
collapse (mean) PV_earnings Experience , by(experiment)
gen age=30
save "Temp\Age30.dta", replace
restore

collapse (mean) PV_earnings Experience , by(experiment education)
gen age=30
append using "Temp\Age30.dta"
save "Temp\Age30.dta", replace
}

* age 40
qui{
clear
use "Experiments Stata Files/Experiment010.dta"
append using "Experiments Stata Files/Experiment014.dta"
append using "Experiments Stata Files/Experiment030.dta"

keep if age<=64
drop if age<40

bysort experiment ID: gen insample=1 if (H==2 | H==3) & age==40
replace insample=0 if insample==.
bysort experiment ID: egen insample_new= max(insample)
keep if insample_new==1

gen Experience=hours/40
sort experiment ID age
by experiment ID: egen  Experience_total = total(Experience)
drop Experience
rename Experience_total Experience
replace Experience=. if age>40

sort experiment ID age
gen time_period = age-40
generate discounting_factor=0.956937799^(time_period)  
gen discounted_earnings=earnings*discounting_factor
bysort experiment ID: egen  PV_earnings = total(discounted_earnings)
replace  PV_earnings=. if age>40

preserve
* all
collapse (mean) PV_earnings Experience , by(experiment)
gen age=40
save "Temp\Age40.dta", replace
restore

collapse (mean) PV_earnings Experience , by(experiment education)
gen age=40
append using "Temp\Age40.dta"
save "Temp\Age40.dta", replace
}

* age 50 
qui{
clear
use "Experiments Stata Files/Experiment011.dta"
append using "Experiments Stata Files/Experiment015.dta"
append using "Experiments Stata Files/Experiment031.dta"

keep if age<=64
drop if age<50

bysort experiment ID: gen insample=1 if (H==2 | H==3) & age==50
replace insample=0 if insample==.
bysort experiment ID: egen insample_new= max(insample)
keep if insample_new==1

gen Experience=hours/40
sort experiment ID age
by experiment ID: egen  Experience_total = total(Experience)
drop Experience
rename Experience_total Experience
replace Experience=. if age>50

sort experiment ID age
gen time_period = age-50
generate discounting_factor=0.956937799^(time_period)  
gen discounted_earnings=earnings*discounting_factor
bysort experiment ID: egen  PV_earnings = total(discounted_earnings)
replace  PV_earnings=. if age>50

preserve
* all
collapse (mean) PV_earnings Experience , by(experiment)
gen age=50
save "Temp\Age50.dta", replace
restore

collapse (mean) PV_earnings Experience , by(experiment education)
gen age=50
append using "Temp\Age50.dta"
save "Temp\Age50.dta", replace
}

* age 60
qui{
clear
use "Experiments Stata Files/Experiment012.dta"
append using "Experiments Stata Files/Experiment016.dta"
append using "Experiments Stata Files/Experiment032.dta"

keep if age<=64
drop if age<60

bysort experiment ID: gen insample=1 if (H==2 | H==3) & age==60
replace insample=0 if insample==.
bysort experiment ID: egen insample_new= max(insample)
keep if insample_new==1

gen Experience=hours/40
sort experiment ID age
by experiment ID: egen  Experience_total = total(Experience)
drop Experience
rename Experience_total Experience
replace Experience=. if age>60

sort experiment ID age
gen time_period = age-60
generate discounting_factor=0.956937799^(time_period)  
gen discounted_earnings=earnings*discounting_factor
bysort experiment ID: egen  PV_earnings = total(discounted_earnings)
replace  PV_earnings=. if age>60

preserve
* all
collapse (mean) PV_earnings Experience , by(experiment)
gen age=60
save "Temp\Age60.dta", replace
restore

collapse (mean) PV_earnings Experience , by(experiment education)
gen age=60
append using "Temp\Age60.dta"
save "Temp\Age60.dta", replace

}


clear
use "Temp\Age30.dta"
append using "Temp\Age40.dta"
append using "Temp\Age50.dta"
append using "Temp\Age60.dta"



sort  education age
reshape wide PV_earnings Experience, i(education age) j(experiment)
gen PV_earnings1=PV_earnings9 if age==30
replace PV_earnings1=PV_earnings10 if age==40
replace PV_earnings1=PV_earnings11 if age==50
replace PV_earnings1=PV_earnings12 if age==60

gen PV_earnings2=PV_earnings29 if age==30
replace PV_earnings2=PV_earnings30 if age==40
replace PV_earnings2=PV_earnings31 if age==50
replace PV_earnings2=PV_earnings32 if age==60

gen PV_earnings3=PV_earnings13 if age==30
replace PV_earnings3=PV_earnings14 if age==40
replace PV_earnings3=PV_earnings15 if age==50
replace PV_earnings3=PV_earnings16 if age==60

gen Experience1=Experience9 if age==30
replace Experience1=Experience10 if age==40
replace Experience1=Experience11 if age==50
replace Experience1=Experience12 if age==60

gen Experience2=Experience29 if age==30
replace Experience2=Experience30 if age==40
replace Experience2=Experience31 if age==50
replace Experience2=Experience32 if age==60

gen Experience3=Experience13 if age==30
replace Experience3=Experience14 if age==40
replace Experience3=Experience15 if age==50
replace Experience3=Experience16 if age==60

keep education age PV_earnings1 PV_earnings2 PV_earnings3 Experience1 Experience2  Experience3

drop if education==.
gen col1=PV_earnings2 - PV_earnings1
gen col2=col1/ PV_earnings1 * 100
gen col3=PV_earnings3 - PV_earnings1
gen col4=col3/ PV_earnings1 * 100
gen col5=(col3-col1) /col3*100


gen col6 =Experience2-Experience1  
gen col7 =Experience3-Experience1 


keep age education col*
order age education col*
* open the data set and copy paste into table. All columns and rows are in order. 
}



* Figure 1 
* FIGURE WITH WAGE OFFERS, EMPLOYMENT, EARNINGS, AND EMPLOYMENT OFFERS FOLLOWING HEALTH SHOCK AT 40
qui{
clear 
use "Experiments Stata Files/Experiment010.dta"
append using "Experiments Stata Files/Experiment014.dta"
append using "Experiments Stata Files/Experiment018.dta"
append using "Experiments Stata Files/Experiment027.dta"
* shock at age 40
keep if age<65
drop if age<40

bysort experiment ID: gen insample=1 if (H==2 | H==3) & age==40
replace insample=0 if insample==.
bysort experiment ID: egen insample_new= max(insample)

keep if insample_new==1


gen I_good = 0 
replace I_good =1 if H==3

gen I_fair = 0 
replace I_fair =1 if H==2

table offer if experiment==10 & education==1 & productivity_type==3 & age==40, stat(mean wage_offer)
gen I_good_job_offer=0
replace I_good_job_offer=1 if offer==5

gen I_FT_job_offer=0
replace I_FT_job_offer=1 if offer==5 | offer==4

replace employed_yn=0 if offer_accepted==2 | offer_accepted==3 // we want to look only at FT employment

collapse (mean) Wages=wage_offer I_good I_fair earnings employed_yn I_good_job_offer I_FT_job_offer, by(experiment age)
gen age_shock=40

table experiment if age==42, stat(mean I_FT_job_offer)


#delimit ;
line  Wages age if experiment==10 & age_shock==40 & age<=50, yaxis(1) xaxis(1) lwidth(medthick) lcolor(edkblue) 
|| line  Wages age if experiment==18 & age_shock==40 & age<=50, lwidth(medthick) lcolor(edkblue)   lpattern(dash)
|| line Wages age if experiment==14 & age_shock==40 & age<=50, lwidth(medthick) lcolor(cranberry)  
  ||, 
  ylabel(21.0(1.0)27.0)
  xlabel(40(2)50)
  ytitle("Average wage offers")
xtitle("Age")
   title("Wage Offers", color(black))
  legend(label(1 "No Severe Health Shock at Age 40")  label(2 "Severe Health Shock at Age 40, HC fixed (as if no shock)") label(3 "Severe Health Shock at Age 40")  )
  legend(col(1) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
    xlabel(,nogrid)
  ;
#delimit cr

graph export "${out_figures}\Wage_offer_shocks_at_40.eps", replace	



replace earnings = earnings/1000


#delimit ;
line  earnings age if experiment==10 & age_shock==40 & age<=50, yaxis(1) xaxis(1) lwidth(medthick) lcolor(edkblue) 
|| line  earnings age if experiment==27 & age_shock==40 & age<=50, lwidth(medthick) lcolor(cranberry)  lpattern(dash)
|| line earnings age if experiment==14 & age_shock==40 & age<=50, lwidth(medthick) lcolor(cranberry)  
  ||, 
  ylabel(35.0(5.0)50.0)
  xlabel(40(2)50)
  ytitle("Average Earnings (thousands)")
xtitle("Age")
   title("Average Earnings", color(black))
  legend(label(1 "No Severe Health Shock at Age 40") label(2 "Severe Health Shock at Age 40, Emp Offers as if No Shocks")  label(3 "Severe Health Shock at Age 40")  )
  legend(col(1) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
   xlabel(,nogrid)
  ;
#delimit cr
graph export "${out_figures}\Earnings_shocks_at_40.eps", replace
	



#delimit ;
line  employed_yn age if experiment==10 & age_shock==40 & age<=50, yaxis(1) xaxis(1) lwidth(medthick) lcolor(edkblue) 
|| line   employed_yn age if experiment==27 & age_shock==40 & age<=50, lwidth(medthick) lcolor(cranberry)  lpattern(dash)
|| line employed_yn age if experiment==14 & age_shock==40 & age<=50, lwidth(medthick) lcolor(cranberry)  
  ||, 
  ylabel(0.8(0.05)1.0)
  xlabel(40(2)50)
  ytitle("Fraction Employed")
xtitle("Age")
   title("FT Employment Rate", color(black))
  legend(label(1 "No Severe Health Shock at Age 40") label(2 "Severe Health Shock at Age 40, Emp Offers as if No Shocks")  label(3 "Severe Health Shock at Age 40")  )
  legend(col(1) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
   xlabel(,nogrid)
  ;
#delimit cr
graph export "${out_figures}\Employment_shocks_at_40.eps", replace	



#delimit ;
line  I_FT_job_offer age if experiment==10 & age_shock==40 & age<=50, yaxis(1) xaxis(1) lwidth(medthick) lcolor(edkblue) 
|| line I_FT_job_offer age if experiment==14 & age_shock==40 & age<=50, lwidth(medthick) lcolor(cranberry)  
  ||, 
  ylabel(0.8(0.05)1.0)
  xlabel(40(2)50)
  ytitle("Fraction")
xtitle("Age")
   title("Fraction with FT Job Offers", color(black))
  legend(label(1 "No Severe Health Shock at Age 40")  label(2 "Severe Health Shock at Age 40")  label(3 ""))
  legend(col(1) rows(3) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  xlabel(,nogrid) 
  legend(rowgap(5.5))
  ;
#delimit cr
graph export "${out_figures}\Offers_shocks_at_40.eps",  replace	

}

* Figure 2 
* by education
qui{
clear 
use "Experiments Stata Files/Experiment010.dta"
append using "Experiments Stata Files/Experiment014.dta"
append using "Experiments Stata Files/Experiment018.dta"
append using "Experiments Stata Files/Experiment027.dta"
* shock at age 40
keep if age<65
drop if age<40


bysort experiment ID: gen insample=1 if (H==2 | H==3) & age==40
replace insample=0 if insample==.
bysort experiment ID: egen insample_new= max(insample)
*tab insample
keep if insample_new==1


table offer if experiment==10 & education==1 & productivity_type==3 & age==40, stat(mean wage_offer)
gen I_good_job_offer=0
replace I_good_job_offer=1 if offer==5

gen I_FT_job_offer=0
replace I_FT_job_offer=1 if offer==5 | offer==4

replace employed_yn=0 if offer_accepted==2 | offer_accepted==3 


collapse (mean) Wages=wage_offer  earnings employed_yn I_good_job_offer I_FT_job_offer, by(experiment age education)
gen age_shock=40


#delimit ;
line  employed_yn age if experiment==10 & age_shock==40 & age<=50 & education==1, yaxis(1) xaxis(1) lwidth(medthick) lcolor(edkblue) 
|| line   employed_yn age if experiment==27 & age_shock==40 & age<=50 & education==1, lwidth(medthick) lcolor(cranberry)  lpattern(dash)
|| line employed_yn age if experiment==14 & age_shock==40 & age<=50 & education==1, lwidth(medthick) lcolor(cranberry)  
  ||, 
  ylabel(0.75(0.05)1.0)
  xlabel(40(2)50)
  ytitle("Fraction Employed")
xtitle("Age")
   title("FT Employment Rate, HS or Less", color(black))
  legend(label(1 "No Severe Health Shock at Age 40") label(2 "Severe Health Shock at Age 40, Emp Offers as if No Shocks")  label(3 "Severe Health Shock at Age 40")  )
  legend(col(1) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr
graph export "${out_figures}\Employment_shocks_at_40_HS.eps", replace	


#delimit ;
line  employed_yn age if experiment==10 & age_shock==40 & age<=50 & education==2, yaxis(1) xaxis(1) lwidth(medthick) lcolor(edkblue) 
|| line   employed_yn age if experiment==27 & age_shock==40 & age<=50 & education==2, lwidth(medthick) lcolor(cranberry)  lpattern(dash)
|| line employed_yn age if experiment==14 & age_shock==40 & age<=50 & education==2, lwidth(medthick) lcolor(cranberry)  
  ||, 
  ylabel(0.75(0.05)1.0)
  xlabel(40(2)50)
  ytitle("Fraction Employed")
xtitle("Age")
   title("FT Employment Rate, Some College", color(black))
  legend(label(1 "No Severe Health Shock at Age 40") label(2 "Severe Health Shock at Age 40, Emp Offers as if No Shocks")  label(3 "Severe Health Shock at Age 40")  )
  legend(col(1) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr
graph export "${out_figures}\Employment_shocks_at_40_SC.eps", replace	


#delimit ;
line  employed_yn age if experiment==10 & age_shock==40 & age<=50 & education==3, yaxis(1) xaxis(1) lwidth(medthick) lcolor(edkblue) 
|| line   employed_yn age if experiment==27 & age_shock==40 & age<=50 & education==3, lwidth(medthick) lcolor(cranberry)  lpattern(dash)
|| line employed_yn age if experiment==14 & age_shock==40 & age<=50 & education==3, lwidth(medthick) lcolor(cranberry)  
  ||, 
  ylabel(0.75(0.05)1.0)
  xlabel(40(2)50)
  ytitle("Fraction Employed")
xtitle("Age")
   title("FT Employment Rate, College", color(black))
  legend(label(1 "No Severe Health Shock at Age 40") label(2 "Severe Health Shock at Age 40, Emp Offers as if No Shocks")  label(3 "Severe Health Shock at Age 40")  )
  legend(col(1) pos(6) region(lcolor(gs16)))
plotregion(margin(r+7 l+5) style(none))
  graphregion(icolor(white) fcolor(gs14) margin(none ))
  graphregion(color(white)) bgcolor(white)
  ;
#delimit cr
graph export "${out_figures}\Employment_shocks_at_40_C.eps", replace	
}



